package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 处理上传图片的接口
 */
@Controller
@RequestMapping("/image")
public class ImageController {
    /**
     * 处理Ckeditor上传图片的资源
     * return
     */
    @RequestMapping("/upload.do")
    @ResponseBody
    public Object upload(@RequestParam MultipartFile upload){
        Map<String,Object> map = new HashMap<>();
        //判断传输过来的数据是不是以image开头的，是的话就是图片了，并且判断文件的大小必须大于0字节
        if(upload.getContentType().startsWith("image")&&upload.getSize()>0){
            //把上传的图片保存到本地的图库中去
            String url = UploadUtil.upload(upload);
            map.put("uploaded",1);
            map.put("url",url);
        }
        //保存在本地路径之后就将获得的路径返回给页面上去
        return map ;
    }

    //新增一行注释yfuygujgujghujhgjh
}
